package com.peironcely.services.business;

import java.math.BigDecimal;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Logger;

import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.peironcely.services.beans.Valor;

public class BusinessServiceImpl extends HibernateDaoSupport implements BusinessService{
     
     
    private static final Logger logger = Logger.getLogger(BusinessServiceImpl.class.getName());
     
    // Constructor to set hibernate SessionFactory to underlining HibernateDaoSupport
    public BusinessServiceImpl(SessionFactory sessionFactory) {  
            super();
            super.setSessionFactory(sessionFactory);  
    }
     
    // method to create a new Value in database, interesting look at @Transactional tag
    @Transactional (rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
    public String newValor(String name, BigDecimal cotizacion){
         
        //... save and return id or null if goes wrong
    	return null;
    }
     
    // get one "Valor"
    @Transactional (readOnly=true)
    public Valor getValor(Integer id){
        logger.info("getting Valor for idValor:'" + id + "'");
        List<Valor> valores = getHibernateTemplate().find("from Valor where idValor=?",id);
        for (Valor valor : valores) {
        	return valor;
		}
        return null;
    }
     
    // list all "Valor" rows
    @Transactional (readOnly=true)
    @SuppressWarnings("unchecked")
    public List<Valor> listValores(){

        logger.info("getting all rows");
        return getHibernateTemplate().find("from Valor");
    }
     
}